package com.fgba.express.controller;


import com.fgba.express.comm.GlobalConst;
import com.fgba.express.comm.exception.BizException;
import com.fgba.express.comm.result.ResultCode;
import com.fgba.express.core.controller.BaseController;
import com.fgba.express.data.dto.MenuDto;
import com.fgba.express.data.entity.Menu;
import com.fgba.express.data.mapstruct.MenuStruct;
import com.fgba.express.data.search.MenuSearch;
import com.fgba.express.data.service.IMenuService;
import com.fgba.express.data.service.IUserService;
import com.fgba.express.util.BeanContextUtil;
import com.fgba.express.util.NullChecker;
import com.fgba.express.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author fgba
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/security/menus")
@Api(value = "菜单相关操作", tags = "菜单相关操作")
public class MenuController extends BaseController<Menu, MenuDto, MenuSearch, IMenuService, MenuStruct> {
    @Override
    public void createValidation(MenuDto model) {
        if (NullChecker.isNullOrEmpty(model.getName())) {
            log.error("MenuController -> save: name is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrEmpty(model.getTitle())) {
            log.error("MenuController -> save: title is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrEmpty(model.getPerms())) {
            log.error("MenuController -> save: perms is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrEmpty(model.getPath())) {
            log.error("MenuController -> save: path is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrZero(model.getOrderNum())) {
            log.error("MenuController -> save: orderNum is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrEmpty(model.getIcon())) {
            log.error("MenuController -> save: icon is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }

        if (NullChecker.isNullOrZero(model.getType().getCode())) {
            log.error("MenuController -> save: type is null");
            throw new BizException(ResultCode.USER_REQUEST_PARAM_IS_BLANK);
        }
    }

    @Override
    public void beforeSave(Menu entity) throws Exception {
        //为空表明为1级菜单
        if (NullChecker.isNullOrZero(entity.getParentId())) {
            entity.setParentId(0);
        }
    }

    @Override
    public void afterSave(MenuDto model) throws Exception {
        BeanContextUtil.getBean(RedisUtil.class).del(GlobalConst.CONFIG_ATTRIBUTES);
    }

    @Override
    public void afterUpdate(MenuDto model) throws Exception {
        BeanContextUtil.getBean(IUserService.class).clearUserAuthorityInfoByMenuId(model.getId());
    }

    /**
     * 查询角色菜单
     *
     * @return
     */
    @ApiOperation(value = "查询角色菜单", notes = "查询角色菜单")
    @GetMapping("/list-role/{roleId}")
    public List<MenuDto> getMenuByRole(@PathVariable Long roleId) {
        if (NullChecker.isNullOrZero(roleId)) {
            throw new BizException("MenuController.getMenuByRole: roleId is Null");
        }
        return service.getMenuByRoleId(roleId);
    }
}
